{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "discrete-journey",
   "metadata": {},
   "source": [
    "# Brazilian National Identifiers"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "quarterly-airport",
   "metadata": {},
   "source": [
    "## Introduction"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cleared-location",
   "metadata": {},
   "source": [
    "The function `clean_br_cpf()` cleans a column containing Brazilian national identifier (CPF) strings, and standardizes them in a given format. The function `validate_br_cpf()` validates either a single cpf strings, a column of CPF strings or a DataFrame of CPF strings, returning `True` if the value is valid, and `False` otherwise."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "connected-arena",
   "metadata": {},
   "source": [
    "CPF strings can be converted to the following formats via the `output_format` parameter:\n",
    "\n",
    "* `compact`: only number strings without any seperators or whitespace, like \"39053344705\"\n",
    "* `standard`: cpf strings with proper whitespace in the proper places, like \"390.533.447-05\"\n",
    "\n",
    "Invalid parsing is handled with the `errors` parameter:\n",
    "\n",
    "* `coerce` (default): invalid parsing will be set to NaN\n",
    "* `ignore`: invalid parsing will return the input\n",
    "* `raise`: invalid parsing will raise an exception\n",
    "\n",
    "The following sections demonstrate the functionality of `clean_br_cpf()` and `validate_br_cpf()`. "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "permanent-development",
   "metadata": {},
   "source": [
    "### An example dataset containing cpf strings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "exact-regular",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "df = pd.DataFrame(\n",
    "    {\n",
    "        \"cpf\": [\n",
    "            '390.533.447-05',\n",
    "            '231.002.999-00',\n",
    "            '390.533.447=0',\n",
    "            '23100299900',\n",
    "            \"hello\",\n",
    "            np.nan,\n",
    "            \"NULL\"\n",
    "        ], \n",
    "        \"address\": [\n",
    "            \"123 Pine Ave.\",\n",
    "            \"main st\",\n",
    "            \"1234 west main heights 57033\",\n",
    "            \"apt 1 789 s maple rd manhattan\",\n",
    "            \"robie house, 789 north main street\",\n",
    "            \"(staples center) 1111 S Figueroa St, Los Angeles\",\n",
    "            \"hello\",\n",
    "        ]\n",
    "    }\n",
    ")\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "internal-conclusion",
   "metadata": {},
   "source": [
    "## 1. Default `clean_br_cpf`\n",
    "\n",
    "By default, `clean_br_cpf` will clean cpf strings and output them in the standard format with proper separators."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "horizontal-emission",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dataprep.clean import clean_br_cpf\n",
    "clean_br_cpf(df, column = \"cpf\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "victorian-nation",
   "metadata": {},
   "source": [
    "## 2. Output formats"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "vertical-meaning",
   "metadata": {},
   "source": [
    "This section demonstrates the output parameter."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "combined-ecology",
   "metadata": {},
   "source": [
    "### `standard` (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "combined-relation",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_br_cpf(df, column = \"cpf\", output_format=\"standard\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "broad-continent",
   "metadata": {},
   "source": [
    "### `compact`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "flexible-philosophy",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_br_cpf(df, column = \"cpf\", output_format=\"compact\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "efficient-likelihood",
   "metadata": {},
   "source": [
    "## 3. `inplace` parameter\n",
    "\n",
    "This deletes the given column from the returned DataFrame. \n",
    "A new column containing cleaned CPF strings is added with a title in the format `\"{original title}_clean\"`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cultural-approval",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_br_cpf(df, column=\"cpf\", inplace=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "determined-bargain",
   "metadata": {},
   "source": [
    "## 4. `errors` parameter"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "written-episode",
   "metadata": {},
   "source": [
    "### `coerce` (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "adaptive-elephant",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_br_cpf(df, \"cpf\", errors=\"coerce\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "experimental-fight",
   "metadata": {},
   "source": [
    "### `ignore`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "raised-intention",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_br_cpf(df, \"cpf\", errors=\"ignore\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "nutritional-horizontal",
   "metadata": {},
   "source": [
    "## 4. `validate_br_cpf()`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "mighty-crawford",
   "metadata": {},
   "source": [
    "`validate_br_cpf()` returns `True` when the input is a valid CPF. Otherwise it returns `False`.\n",
    "\n",
    "The input of `validate_br_cpf()` can be a string, a Pandas DataSeries, a Dask DataSeries, a Pandas DataFrame and a dask DataFrame.\n",
    "\n",
    "When the input is a string, a Pandas DataSeries or a Dask DataSeries, user doesn't need to specify a column name to be validated. \n",
    "\n",
    "When the input is a Pandas DataFrame or a dask DataFrame, user can both specify or not specify a column name to be validated. If user specify the column name, `validate_br_cpf()` only returns the validation result for the specified column. If user doesn't specify the column name, `validate_br_cpf()` returns the validation result for the whole DataFrame."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bulgarian-matter",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dataprep.clean import validate_br_cpf\n",
    "print(validate_br_cpf('390.533.447-05'))\n",
    "print(validate_br_cpf('231.002.999-00'))\n",
    "print(validate_br_cpf('390.533.447=0'))\n",
    "print(validate_br_cpf('23100299900'))\n",
    "print(validate_br_cpf(\"hello\"))\n",
    "print(validate_br_cpf(np.nan))\n",
    "print(validate_br_cpf(\"NULL\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "exotic-crossing",
   "metadata": {},
   "source": [
    "### Series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "corporate-polyester",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_br_cpf(df[\"cpf\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "first-projector",
   "metadata": {},
   "source": [
    "### DataFrame + Specify Column"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "sunrise-physiology",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_br_cpf(df, column=\"cpf\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "quiet-monitoring",
   "metadata": {},
   "source": [
    "### Only DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "herbal-future",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_br_cpf(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "sapphire-walnut",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
